package com.kcwx.system.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kcwx.common.core.domain.entity.SysDictData;
import com.kcwx.common.utils.DictUtils;
import com.kcwx.common.core.redis.RedisCache;

import com.kcwx.system.mapper.SysDictDataMapper;
import com.kcwx.system.service.ISysDictDataService;

import com.kcwx.shareddomain.system.query.SysDictDataQuery;

/**
 * 字典 业务层处理
 * 
 */
@Service
public class SysDictDataServiceImpl implements ISysDictDataService
{
    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Resource
    private RedisCache redisCache;

    /**
     * 根据条件分页查询字典数据
     * 
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataList(SysDictDataQuery dictData)
    {
        return dictDataMapper.selectDictDataList(dictData);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     * 
     * @param dictType 字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue)
    {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     * 
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode)
    {
        return dictDataMapper.selectDictDataById(dictCode);
    }

    /**
     * 删除字典数据信息
     *
     * @param dictId 需要删除的字典数据ID
     * @param delBy 删除人
     */
    @Override
    public int deleteDictDataById(Long dictId,String delBy)
    {
        SysDictData data=dictDataMapper.selectDictDataById(dictId);
        if(data!=null) {
            int rowsAffect = dictDataMapper.deleteDictDataById(dictId, delBy);
            if (rowsAffect > 0) {
                DictUtils.removeDictCache(data.getDictType());
                redisCache.deleteObject("dict:" + data.getDictType());
                List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
                DictUtils.setDictCache(data.getDictType(), dictDatas);
            }
            return rowsAffect;
        }
        return 1;
    }
    /**
     * 批量删除字典数据信息
     * 
     * @param dictCodes 需要删除的字典数据ID
     * @param delBy 删除人
     */
    @Override
    public int deleteDictDataByIds(Long[] dictCodes,String delBy)
    {
        List<SysDictData> dataList=dictDataMapper.selectDictByIds(dictCodes);
        if(!dataList.isEmpty()) {
            int rowsAffect=dictDataMapper.deleteDictDataByIds(dictCodes,delBy);
            if(rowsAffect>0){
                for(SysDictData data:dataList){
                    DictUtils.removeDictCache(data.getDictType());
                    redisCache.deleteObject("dict:" + data.getDictType());
                    List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
                    DictUtils.setDictCache(data.getDictType(), dictDatas);
                }
            }
            return rowsAffect;
        }
        return 1;
    }

    /**
     * 新增保存字典数据信息
     * 
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictData data)
    {
        int row = dictDataMapper.insertDictData(data);
        if (row > 0)
        {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
        return row;
    }

    /**
     * 修改保存字典数据信息
     * 
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysDictData data)
    {
        int row = dictDataMapper.updateDictData(data);
        if (row > 0)
        {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
            List<SysDictData> list = redisCache.getCacheObject("dict:" + data.getDictType());
            if (ObjectUtil.isNotEmpty(list)) {
                list = dictDataMapper.selectKeyValueByType(data.getDictType());
                redisCache.setCacheObject("dict:" + data.getDictType(), list, 24, TimeUnit.HOURS);
            }
        }
        return row;
    }
}
